package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Employee;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.query.EmployeeQuery;
import com.by.service.EmployeeService;
import com.by.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "员工管理")
@RequestMapping("/v1")
public class EmployeeController extends BaseController {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    UserService userService;

    @ApiOperation("保存员工")
    @PostMapping("/employee")
    public ResultBean save(@Validated(ISave.class) @RequestBody Employee employee, BindingResult result) {
        validate(result);
        //检查用户名是否存在
        int rs = userService.queryUserByAccount(employee.getUser().getUsername());
        if (rs == 1) {
            return fail("该用户名已占用！");
        }
        rs = employeeService.save(employee);
        return saveResult(rs);
    }


    @ApiOperation("员工列表")
    @PostMapping("/employee/query")
    public ResultBean query(@RequestBody EmployeeQuery query) {
        Map map = BeanUtil.beanToMap(query);
        PageBean bean = employeeService.query(map);
        return success("员工列表", bean);
    }

    @ApiOperation("获取所有员工")
    @GetMapping("/employee/getEmployeeList")
    public ResultBean getEmployeeList() {
        List<Map> list = employeeService.getEmployeeList();
        return success("列表",list);

    }

}
